package me.obstsalat.guildera;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;

public class worldLib {
	private static World world = null;
	private static guildera plugin = null;
	
	public static final int SURFACE_HIGHEST = 1, SURFACE_LOWEST = 2;
	public static final int SURFACE2_NONE = 1, SURFACE2_IGNORELIQUID = 2;
	
	public static void initializeInstance(guildera instance) {
		plugin = instance;
	}
	
	public static void initializeWorld(World instance) {
		world = instance;
	}
	
	public static int getSurface(int x, int z, int SURFACE, int SURFACE2) {
		checkInitialization();
		
		boolean cont = true;
		int re = -1;
		int m = 1;
		if(SURFACE == SURFACE_HIGHEST || SURFACE2 == SURFACE2_IGNORELIQUID) {
			re = 129;
			m = -1;
		} else if (SURFACE == SURFACE_LOWEST || SURFACE2 == SURFACE2_IGNORELIQUID) {
			re = 0;
			m = 1;
		}
		
		do {
			re = re + m;
			Block bl = world.getBlockAt(x,re,z);
			
			if(SURFACE == SURFACE_HIGHEST && SURFACE2 == SURFACE2_IGNORELIQUID && bl.getType() != Material.AIR && !bl.isLiquid()) {
				re += 1;
				cont = false;
			} else if(SURFACE == SURFACE_HIGHEST && bl.getType() != Material.AIR) {
				re += 1;
				cont = false;
			} else if(SURFACE == SURFACE_LOWEST && SURFACE2 == SURFACE2_IGNORELIQUID && (bl.getType() == Material.AIR || bl.isLiquid())) {
				cont = false;
			} else if(SURFACE == SURFACE_LOWEST && bl.getType() == Material.AIR) {
				cont = false;
			}
			
			
			
			if(re > 128 || re < 4) { //Out of Range!
				cont = false;
			}
		} while(cont);

		return re;
	}
	
	public static int[] getAllSurfaces(int x, int z, int SURFACE2) {
		List<Integer> cache = new ArrayList<Integer>();
		int i = 129;
		int l = 0;
		boolean last_air = false;
		
		do {
			i--;
			if( world.getBlockAt(x,i,z).getTypeId() == 0 || (SURFACE2 == SURFACE2_IGNORELIQUID && world.getBlockAt(x,i,z).isLiquid()) ) {
				last_air = true;
			} else if(last_air){
				cache.add(i);
				l++;
				last_air = false;
			}
			
		} while(i > 4);
		
		if(l>0) {
			Integer[] ret = cache.toArray(new Integer[cache.size()]);
			int[] r = new int[ret.length];
			for(Integer inte = 0; inte < ret.length; inte++) {
				r[inte] = ret[inte].intValue();
			}
			return r;
		} else {
			return null;
		}
		
	}
	
	public static void spawnBlock(int x, int y, int z, int id) {
		checkInitialization();
		
		world.getBlockAt(x,y,z).setTypeId(id);
	}
	
	public static void spawnBlock(int x, int y, int z, int id, int data) {
		checkInitialization();
		
		world.getBlockAt(x,y,z).setTypeId(id);
		world.getBlockAt(x,y,z).setData((byte)data);
	}
	
	public static int getBlockId(int x, int y, int z) {
		checkInitialization();
		
		return world.getBlockAt(x,y,z).getTypeId();
	}
	
	public static void setBlockData(int x, int y, int z, int data) {
		checkInitialization();
		
		world.getBlockAt(x,y,z).setData((byte)data);
	}
	
	public static int getBlockData(int x, int y, int z) {
		checkInitialization();
		
		return (int)world.getBlockAt(x,y,z).getData();
	}
	
	private static boolean checkInitialization() {
		try {
			if(world == plugin.overworld) {
				return true;
			} else {
				plugin.logger.info("WARNING: GuildEra.worldLib.java:checkInitialization() worldLib wasnt initialized correctly. A crash can occure.");
				return false;
			}
		} catch(Exception ex) {
			plugin.logger.info("WARNING: GuildEra.worldLib.java:checkInitialization() worldLib wasnt initialized correctly. A crash can occure.");
			return false;
		}
	}
}
